<template>
  <a-modal
    :visible="visible"
    title="总分计算配置"
    @cancel="cancel"
    @ok="add"
    okText="保存"
  >
    <a-table
      :loading="loading"
      bordered
      :pagination="false"
      :columns="columns"
      :dataSource="tableData.checkRecordMarkSheetDTOS"
    >
      <div class="flex align-items-center" slot="input" slot-scope="text,record">
        <b-input-number v-model="record.accounted"
                        style="flex: 1;margin-right: 20px;"
                        class="requiredFormItem"
        />
        <span>%</span>
      </div>
    </a-table>
  </a-modal>
</template>
<script>
import Api from '@/api/safetySupervision/base/checkLogScore.js'

export default {
  name: 'totalScoreCalcModal',
  components: {},
  props: {
    visible: false,
    sourceData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      columns: [
        { title: '分类', dataIndex: 'checkName' },
        { title: '占比', dataIndex: 'accounted', scopedSlots: { customRender: 'input' } }
      ],
      tableData: { checkRecordMarkSheetDTOS: [] }
    }
  },
  computed: {},
  watch: {
    visible(newVal) {
      if (newVal) {
        this.tableData = this.$clone(this.sourceData)
      }
    }
  },
  created() {
  },
  methods: {
    cancel() {
      this.$emit('update:visible', false)
    },
    async add() {
      for (let i = 0; i < this.tableData.checkRecordMarkSheetDTOS.length; i++) {
        if (!this.tableData.checkRecordMarkSheetDTOS[i].accounted) {
          return this.$noticeWarning('请填写完整')
        }
      }
      let temp = this.$clone(this.tableData)
      temp.checkRecordMarkSheetDTOS.map(v => {
        v.checkRecordMarkSheetDetailsDTOS.map(w => {
          delete w.id
        })
      })
      this.loading = true
      let res = await Api.editByMain({}, temp)
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
      // if (true) {
        this.$noticeSuccess('保存成功')
        this.cancel()
        this.$emit('ok', this.tableData)
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
